﻿using MB.RuleBase.Common;
using Model;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class OffShelvesOrderBll
    {
        public bool SetOffMiddleShelves(string userID, out string message, List<pft_middle_goodsinventory> list)
        {
            bool outPut = false;
            int Userid = 3;//默认管理员
            UserInfo user = new LoginBll().GetUserIDByUserCode(userID.Trim());
            if (user != null)
            { Userid = user.USERID; }
            message = "";
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;
            List<Pft_goodsinventory> LstPFTStock = new List<Pft_goodsinventory>();//分拣仓自动上架
            List<PFT_goodsinventoryHis> LstPFTStockHis = new List<PFT_goodsinventoryHis>();//分拣仓自动上架记录
            List<pft_middle_goodsinventoryhis> LstmiddlePFTStockHis = new List<pft_middle_goodsinventoryhis>();
            try
            {
                // 打开数据连接事物
                cn.Open();
                tran = cn.BeginTransaction();
                foreach (var item in list)
                {
                    int TopGoodsid = 0;
                    //if (_OffShelves.GOODSCODE.Contains("WOC"))
                    //    TopGoodsid = _OffShelves.GOODSID;
                    //else
                    //    TopGoodsid = GetTopGoodsidByGoodsid(_OffShelves.GOODSID);
                    TopGoodsid = item.GOODSID;
                    if (TopGoodsid != 0)
                    {
                        Pft_goodsinventory PFTItem = new Pft_goodsinventory();
                        PFTItem.GOODSID = TopGoodsid;
                        PFTItem.CREATERID = Userid;
                        PFTItem.STATUS = 1;
                        PFTItem.QTY = item.QTY;//取整转换
                        item.QTY = -item.QTY;//将数量变为负数，供中建仓减库存
                        if (PFTItem.QTY != 0)
                            LstPFTStock.Add(PFTItem);
                        LstPFTStockHis.Add(new PFT_goodsinventoryHis() { GOODSID = TopGoodsid, OFFSHELVESID = 0, LMOFFSHELVESGOODSID = item.GOODSID, CREATERID = Userid, QTY = item.QTY, NOTE = "自动上架" });
                        LstmiddlePFTStockHis.Add(new pft_middle_goodsinventoryhis() { GOODSID = TopGoodsid, OFFSHELVESID = 0, LMOFFSHELVESGOODSID = 0, CREATERID = Userid, QTY = item.QTY, NOTE = "摘果下架" });
                    }
                }
                if (LstPFTStock.Count > 0)
                {
                    MB.Util.TraceEx.Write("上架分拣库存进行中。。。。。，信息数量：" + LstPFTStock.Count.ToString());
                    outPut = Insertgoodsinventory(tran, LstPFTStock);
                    if (LstPFTStockHis.Count > 0)
                        InsertgoodsinventoryHIS(tran, LstPFTStockHis);
                }
                if (outPut && list.Count > 0)
                {
                    outPut = outPut && InsertMiddlegoodsinventory(tran, list);
                    if (LstmiddlePFTStockHis.Count > 0)
                        InsertMiddlegoodsinventoryHIS(tran, LstmiddlePFTStockHis);
                }
                //提交事务
                if (outPut)
                {
                    tran.Commit();
                }
                else
                {
                    tran.Rollback();
                }

                return outPut;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                message = "中间仓下架失败！\n" + ex.Message;
                return outPut;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
        }

        public pft_middle_inventoryTMP GetMiddleGoodsInfoByBarCode(string barCode)
        {
            try
            {
                pft_middle_inventoryTMP _goods = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<pft_middle_inventoryTMP>("Goodsinventory", "GetMiddleGoodsInfoByBarCode", new object[] { barCode }).FirstOrDefault();

                if (_goods != null)
                    return _goods;
                else
                    return null;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }

        public List<pft_middle_inventoryTMP> GetMiddleGoodsInfoByBarName(string GoodsName)
        {
            try
            {
                GoodsName = "%" + GoodsName + "%";
                List<pft_middle_inventoryTMP> _goods = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<pft_middle_inventoryTMP>("Goodsinventory", "GetMiddleGoodsInfoByBarName", new object[] { GoodsName });

                if (_goods != null && _goods.Count>0)
                    return _goods;
                else
                    return null;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 摘果仓自动上架
        /// </summary>
        /// <param name="Goodsinventory"></param>
        /// <returns></returns>
        private bool Insertgoodsinventory(DbTransaction transaction, List<Pft_goodsinventory> Goodsinventory)
        {
            try
            {
                if (Goodsinventory == null || Goodsinventory.Count <= 0) return false;

                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(transaction))
                {
                    bulk.WriteToServer("Goodsinventory", "InsertGoodsinventory", Goodsinventory);
                }

                return true;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        }
        /// <summary>
        /// 中间仓自动上架
        /// </summary>
        /// <param name="transaction"></param>
        /// <param name="MiddleGoodsinventory"></param>
        /// <returns></returns>
        private bool InsertMiddlegoodsinventory(DbTransaction transaction, List<pft_middle_goodsinventory> MiddleGoodsinventory)
        {
            try
            {
                if (MiddleGoodsinventory == null || MiddleGoodsinventory.Count <= 0) return false;

                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(transaction))
                {
                    bulk.WriteToServer("Goodsinventory", "InsertMiddleGoodsinventory", MiddleGoodsinventory);
                }

                return true;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        }
        /// <summary>
        /// 摘果仓历史记录
        /// </summary>
        /// <param name="transaction"></param>
        /// <param name="GoodsinventoryHis"></param>
        /// <returns></returns>
        private bool InsertgoodsinventoryHIS(DbTransaction transaction, List<PFT_goodsinventoryHis> GoodsinventoryHis)
        {

            try
            {
                if (GoodsinventoryHis == null || GoodsinventoryHis.Count <= 0) return false;

                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(transaction))
                {
                    bulk.WriteToServer("Goodsinventory", "InsertGoodsinventoryHIS", GoodsinventoryHis);
                }

                return true;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        }
        /// <summary>
        /// 中间仓历史记录
        /// </summary>
        /// <param name="transaction"></param>
        /// <param name="MiddleGoodsinventoryHis"></param>
        /// <returns></returns>
        private bool InsertMiddlegoodsinventoryHIS(DbTransaction transaction, List<pft_middle_goodsinventoryhis> MiddleGoodsinventoryHis)
        {

            try
            {
                if (MiddleGoodsinventoryHis == null || MiddleGoodsinventoryHis.Count <= 0) return false;

                using (MB.RuleBase.BulkCopy.IDbBulkExecute bulk = MB.RuleBase.BulkCopy.DbBulkExecuteFactory.CreateDbBulkExecute(transaction))
                {
                    bulk.WriteToServer("Goodsinventory", "InsertMiddleGoodsinventoryHIS", MiddleGoodsinventoryHis);
                }

                return true;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        }
    }
}
